package sg.edu.nus.iss.jass.gui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Label;
import java.awt.event.ActionListener;
import java.util.Properties;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import sg.edu.nus.iss.jass.model.ResponseDTO;
import sg.edu.nus.iss.jass.util.GUIUtils;

/**
 * @version 1.0
 * @author SE19 Team 1
 * 
 */
public class ViewResponseDetailPanel extends JPanel {
	private static final long serialVersionUID = 1L;

	public static final String BTN_BACK_TO_RESPONSE_LIST = "btnBackToResponseList";
	
	private int numRows = 0;
	private Properties properties;
	private JPanel headerPanel;
	private JPanel detailPanel;

	private JLabel msgLabel;
	private JLabel lblCandidateName;
	private JLabel lblCandidateEmail;
	private JLabel lblCandidateAddress;
	private JLabel lblCandidateTelNo;
	private JLabel lblCandidateResumeTitle;
	private JLabel lblCandidateResume;
	private JLabel lblJobAdvertName;
	private JLabel lblJobAdvertDescription;
	private JLabel lblResponseDate;

	private JTextField txtCandidateName;
	private JTextField txtCandidateEmail;
	private JTextField txtCandidateTelNo;
	private JTextField txtCandidateResumeTitle;
	private JTextField txtJobAdvertName;
	private JTextArea txtJobAdvertDescription;
	private JTextField txtResponseDate;

	private JTextArea txtCandidateAddress;
	private JTextArea txtCandidateResume;

	private JButton btnBackToResponseList;
	private ResponseDTO response;

	public ViewResponseDetailPanel() {
		try {
			this.properties = GUIUtils.loadConfiguration();
			this.initResponseDetailScreen();
		} catch (Exception ex) {
			this.msgLabel.setText(ex.getMessage());
		}
	}

	public ViewResponseDetailPanel(ResponseDTO response) {

		this.response = response;
		if (response != null)
			this.displayDetailPanel();
	}

	public void addComponentActionListener(ActionListener listener) {
		this.btnBackToResponseList.addActionListener(listener);
	}

	public ResponseDTO getResponse() {
		return this.response;
	}

	public void setResponse(ResponseDTO response) {
		this.response = response;
		if (response != null)
			this.displayDetailPanel();
	}

	private void addRow(JPanel panel, Component label, Component component) {
		GridBagConstraints constraints = new GridBagConstraints();
		constraints.insets = new Insets(5, 5, 5, 5);

		if (label != null) {
			constraints.gridx = 0;
			constraints.gridy = this.numRows;
			panel.add(label, constraints);
		}

		if (component != null) {
			constraints.gridx = 1;
			constraints.gridy = this.numRows;
			panel.add(component, constraints);
		}

		this.numRows++;
	}

	private void displayDetailPanel() {
		this.txtCandidateName.setText(this.response.getCandidateName());
		this.txtCandidateEmail.setText(this.response.getCandidateEmail());
		this.txtCandidateAddress.setText(this.response.getCandidateAddress());
		this.txtCandidateAddress.setAutoscrolls(true);
		this.txtCandidateTelNo.setText(this.response.getCandidateTelNo());
		this.txtCandidateResumeTitle.setText(this.response.getCandidateResumeTitle());
		this.txtCandidateResume.setText(this.response.getCandidateResume());
		this.txtJobAdvertName.setText(this.response.getJobAdvert().getJobTitle());
		this.txtJobAdvertDescription.setText(this.response.getJobAdvert().getJobDescription());
		this.txtResponseDate.setText(this.response.getResponseDate().toString());

	}

	private JPanel getDetailPanel() {
		this.detailPanel = new JPanel();
		GridBagLayout layout = new GridBagLayout();
		this.detailPanel.setLayout(layout);
		this.detailPanel.setOpaque(false);

		this.lblJobAdvertName = GUIUtils.createLabel(this.properties.getProperty("label.response.jobAdvertName"));
		this.txtJobAdvertName = GUIUtils.createTextField();
		this.txtJobAdvertName.setEditable(false);
		this.addRow(this.detailPanel, this.lblJobAdvertName, this.txtJobAdvertName);

		this.lblJobAdvertDescription = GUIUtils.createLabel(this.properties
				.getProperty("label.response.jobAdvertDescription"));
		this.txtJobAdvertDescription = GUIUtils.createTextArea();
		this.txtJobAdvertDescription.setEditable(false);
		this.addRow(this.detailPanel, this.lblJobAdvertDescription, this.txtJobAdvertDescription);

		this.lblCandidateName = GUIUtils.createLabel(this.properties.getProperty("label.response.candidateName"));
		this.txtCandidateName = GUIUtils.createTextField();
		this.txtCandidateName.setEditable(false);
		this.addRow(this.detailPanel, this.lblCandidateName, this.txtCandidateName);

		this.lblCandidateEmail = GUIUtils.createLabel(this.properties.getProperty("label.response.candidateEmail"));
		this.txtCandidateEmail = GUIUtils.createTextField();
		this.txtCandidateEmail.setEditable(false);
		this.addRow(this.detailPanel, this.lblCandidateEmail, this.txtCandidateEmail);

		this.lblCandidateAddress = GUIUtils.createLabel(this.properties.getProperty("label.response.candidateAddress"));
		this.txtCandidateAddress = GUIUtils.createTextArea();
		this.txtCandidateAddress.setEditable(false);
		this.addRow(this.detailPanel, this.lblCandidateAddress, this.txtCandidateAddress);

		this.lblCandidateTelNo = GUIUtils.createLabel(this.properties.getProperty("label.response.candidateTelNo"));
		this.txtCandidateTelNo = GUIUtils.createTextField();
		this.txtCandidateTelNo.setEditable(false);
		this.addRow(this.detailPanel, this.lblCandidateTelNo, this.txtCandidateTelNo);

		this.lblCandidateResumeTitle = GUIUtils.createLabel(this.properties
				.getProperty("label.response.candidateResumeTitle"));
		this.txtCandidateResumeTitle = GUIUtils.createTextField();
		this.txtCandidateResumeTitle.setEditable(false);
		this.addRow(this.detailPanel, this.lblCandidateResumeTitle, this.txtCandidateResumeTitle);

		this.lblCandidateResume = GUIUtils.createLabel(this.properties.getProperty("label.response.candidateResume"));
		this.txtCandidateResume = GUIUtils.createTextArea();

		this.txtCandidateResume.setEditable(false);

		this.addRow(this.detailPanel, this.lblCandidateResume, new JScrollPane(this.txtCandidateResume));

		this.lblResponseDate = GUIUtils.createLabel(this.properties.getProperty("label.response.responseDate"));
		this.txtResponseDate = GUIUtils.createTextField();
		this.addRow(this.detailPanel, this.lblResponseDate, this.txtResponseDate);

		this.btnBackToResponseList = new JButton(this.properties.getProperty("button.label.backToResponseList"));
		this.btnBackToResponseList.setName(BTN_BACK_TO_RESPONSE_LIST);
		this.btnBackToResponseList.setPreferredSize(new Dimension(175, 25));
		this.addRow(this.detailPanel, new Label(), this.btnBackToResponseList);

		return this.detailPanel;
	}

	private JPanel getHeaderPanel() {
		this.headerPanel = new JPanel();
		this.headerPanel.setLayout(new GridLayout(0, 1, 10, 10));
		this.headerPanel.setOpaque(false);

		this.msgLabel = new JLabel(this.properties.getProperty("title.viewResponse.responseDetail"));
		this.msgLabel.setFont(new Font("Calibri", Font.PLAIN, 30));
		this.msgLabel.setHorizontalAlignment(SwingConstants.CENTER);
		this.headerPanel.add(this.msgLabel);

		ImageIcon imgDivider = new ImageIcon("./images/divider.jpg");
		JLabel lblDivider = new JLabel(imgDivider);
		this.headerPanel.add(lblDivider);

		return this.headerPanel;
	}

	private void initResponseDetailScreen() {
		this.setLayout(new BorderLayout());
		this.add(this.getHeaderPanel(), BorderLayout.NORTH);
		this.add(this.getDetailPanel(), BorderLayout.CENTER);
	}
}
